/** 
* All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
* 
* PmCancelApplyService
* 项目取消申请
* 
* 修改纪录
* 2020-05-20 版本：1.0 shenyouyi 创建。
* @version 版本：1.0
* @author 作者：shenyouyi
* 创建日期：2020-05-20
*/
package com.topscomm.pm.service.impl;

import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.tap.common.DatacodeCache;
import com.topscomm.tap.common.TapSystemConst;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.pm.service.IPmCancelApplyService;
import com.topscomm.pm.service.impl.auto.PmCancelApplyServiceAuto;
import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.util.PrimaryKeyUtil;
import com.topscomm.pm.pojo.PmCancelApplyEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(value = "pmCancelApplyService")
@Scope(value = "singleton")
public class PmCancelApplyService extends PmCancelApplyServiceAuto implements IPmCancelApplyService {
	/**
	 * @author: shenyouyi
	 * @date: 2020-05-20
	 * @param entity
	 * @description:单号生成
	 * @modify:
	 */
	@Override
	protected void beforeInsert(BasicEntity entity){		
		super.beforeInsert(entity);
		if (entity.getId() <= 0) {// 赋值Id和单号
			long[] arrayId = PrimaryKeyUtil.GeneIDs(entity.getTableName(), 1);
			entity.setId(arrayId[0]);
			String docNo = String.valueOf(arrayId[0]);
			docNo = docNo.substring(0, 6) + docNo.substring(9);
			entity.setValue(PmCancelApplyEntity.FieldDocno, docNo);
		}
	}

	/**
	  * @author: shenyouyi
	  * @date: 2020/6/16
	  * @param
	  * @description: 根据项目ID查询正在申请的单据信息
	  **/
	@Override
	public String queryApplyingsByProjectId(Long projectId, Long cancelApplyId) {
		DatacodeCache datacodeCache = (DatacodeCache) SpringUtil.getBean("datacodeCache");
		List<Map<String, Object>> codeMapList = datacodeCache.getCodeListMap("PmProjectApplyCheckTypes");
		List<Map<String, Object>> applyingsMapList = new ArrayList<>();
		String tableName, tableDesc;
		StringBufferProxy sql = new StringBufferProxy();
		StringBufferProxy result = new StringBufferProxy();
		List<String> docnoList = new ArrayList<>();
		String pmCancelApply = "PmCancelApply";
		for(Map<String, Object> codeMap: codeMapList){
			tableName = (String)codeMap.get("code");
			tableDesc = (String)codeMap.get("name");
			sql.clear();
			if(pmCancelApply.equals(tableName)&&cancelApplyId>0){// 退回后再次提交申请
				sql.appendSingle("select docno from {0} where projectid = {1} and state = {2} and id != {3}", tableName, projectId, TapSystemConst.TapApprovalBillStateEnum.Approveing, cancelApplyId);
			}else{
				sql.appendSingle("select docno from {0} where projectid = {1} and state = {2} ", tableName, projectId, TapSystemConst.TapApprovalBillStateEnum.Approveing);
			}
			List<Map<String,Object>> docnoMapList = this.selectExecute(sql.toString());
			docnoList.clear();
			for(Map<String,Object> docnoMap: docnoMapList){
				String docno = (String)docnoMap.get("docno");
				docnoList.add(docno);
			}
			if(docnoList.size()==0){
				continue;
			}
			Map<String,Object> applyingsMap = new HashMap<>();
			applyingsMap.put("type", tableDesc);
			applyingsMap.put("docnos", String.join(",", docnoList));
			applyingsMapList.add(applyingsMap);
		}
		int index = 1;
		for(Map<String,Object> applyingsMap: applyingsMapList){
			String type = (String)applyingsMap.get("type");
			String docnos = (String)applyingsMap.get("docnos");
			result.appendLineSingle("【{0}】{1}，单据编号：{2}   ", index++, type, docnos);
		}
		return result.toString();
	}
}
